package com.analyse.stock.model;

import cn.hutool.core.date.DateUtil;
import com.analyse.stock.constant.StockConstant;
import com.analyse.stock.utils.StockStringUtil;
import com.analyse.stock.vo.FinanceDataVO;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

@Slf4j
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_fund_flow_realtime")
public class FundFlowRealTimeModel extends BaseModel<FundFlowRealTimeModel> {

    private String stockCode;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date tradeTime;

    private BigDecimal price;

    private BigDecimal upDownPercent;

    private BigDecimal mainAmt;
    private BigDecimal mainAmtPercent;
    private BigDecimal moreBigOrderAmt;
    private BigDecimal moreBigOrderPercent;
    private BigDecimal bigOrderAmt;
    private BigDecimal bigOrderPercent;

    private BigDecimal midOrderAmt;

    private BigDecimal midOrderPercent;
    private BigDecimal smallOrderAmt;
    private BigDecimal smallOrderPercent;

    //散户净买金额
    private BigDecimal flowAmt;
    private BigDecimal flowAmtPercent;
    private Integer partitionKey;

    //成交量
    private BigDecimal tradeVolume;
    //成交额
    private BigDecimal tradeAmount;
    //换手率
    private BigDecimal exchangeRate;

    public static FundFlowRealTimeModel transFundFlowTimeModel(FinanceDataVO fundFlowVO){
        FundFlowRealTimeModel model = new FundFlowRealTimeModel();
        model.setTradeTime(new Date());
        String month = StringUtils.leftPad(String.valueOf(DateUtil.month(model.getTradeTime())),2,"0");
        String year = StringUtils.leftPad(String.valueOf(DateUtil.year(model.getTradeTime())),4,"0");
        model.setPartitionKey(Integer.parseInt(String.format("%s%s",year,month)));
        model.setPrice(StockStringUtil.getDecimalValue(fundFlowVO.getF2()));
        model.setStockCode(fundFlowVO.getF12());
        model.setUpDownPercent(StockStringUtil.getDecimalValue(fundFlowVO.getF3()));
        model.setMainAmt(StockStringUtil.getDecimalValue(fundFlowVO.getF62()));

        //散户净买入
        BigDecimal flowAmount = StockStringUtil.getDecimalValue(fundFlowVO.getF78()).add(StockStringUtil.getDecimalValue(fundFlowVO.getF84()));
        model.setFlowAmt(flowAmount);
        if(StringUtils.isNotBlank(fundFlowVO.getF6())){
            BigDecimal f6 = StockStringUtil.getDecimalValue(fundFlowVO.getF6());
            if(f6.compareTo(BigDecimal.ZERO)>0){
                BigDecimal flowAmtPercent = flowAmount.divide(f6,4,RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                model.setFlowAmtPercent(flowAmtPercent);
                BigDecimal mainAmtPercent = model.getMainAmt().divide(f6,4,RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                model.setMainAmtPercent(mainAmtPercent);
            }
        }
        model.setExchangeRate(StockStringUtil.getDecimalValue(fundFlowVO.getF8()));
        model.setTradeVolume(StockStringUtil.getDecimalValue(fundFlowVO.getF5()));
        model.setTradeAmount(StockStringUtil.getDecimalValue(fundFlowVO.getF6()));
        model.setMoreBigOrderAmt(StockStringUtil.getDecimalValue(fundFlowVO.getF66()));
        model.setMoreBigOrderPercent(StockStringUtil.getDecimalValue(fundFlowVO.getF69()));
        model.setBigOrderAmt(StockStringUtil.getDecimalValue(fundFlowVO.getF72()));
        model.setBigOrderPercent(StockStringUtil.getDecimalValue(fundFlowVO.getF75()));
        model.setMidOrderAmt(StockStringUtil.getDecimalValue(fundFlowVO.getF78()));
        model.setMidOrderPercent(StockStringUtil.getDecimalValue(fundFlowVO.getF81()));
        model.setSmallOrderAmt(StockStringUtil.getDecimalValue(fundFlowVO.getF84()));
        model.setSmallOrderPercent(StockStringUtil.getDecimalValue(fundFlowVO.getF87()));
        if(!StockConstant.preTradeAmtMap.containsKey(model.getStockCode())){
            StockConstant.preTradeAmtMap.put(model.getStockCode(),model.getMainAmt());
        }
        BigDecimal pre = model.getMainAmt().subtract(StockConstant.preTradeAmtMap.get(model.getStockCode()));
        /*log.info("股票：{}--{}，现价：{}元，涨跌幅：{}% ,换手率：{}%，主力净买入：{}万元,主力相比前1min净买入：{}万元,散户净买入：{}万元",model.getStockCode(),
                fundFlowVO.getF14(),model.getPrice(), model.getUpDownPercent(),
                model.getExchangeRate(),
                model.getMainAmt().divide(new BigDecimal(10000),2,RoundingMode.HALF_UP),
                pre.divide(new BigDecimal(10000),2,RoundingMode.HALF_UP),
                model.getFlowAmt().divide(new BigDecimal(10000),2,RoundingMode.HALF_UP));*/
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        StockConstant.preTradeAmtMap.put(model.getStockCode(),model.getMainAmt());
        return model;
    }

    public static FundFlowRealTimeModel transFundFlowTimeModel(String[] str,String stockCode){
        FundFlowRealTimeModel model = new FundFlowRealTimeModel();
        model.setTradeTime(DateUtil.parseDate(str[0]));
        model.setPartitionKey(DateUtil.year(model.getTradeTime()));
        model.setMainAmt(StockStringUtil.getDecimalValue(str[1]));
        model.setSmallOrderAmt(StockStringUtil.getDecimalValue(str[2]));
        model.setMidOrderAmt(StockStringUtil.getDecimalValue(str[3]));
        model.setBigOrderAmt(StockStringUtil.getDecimalValue(str[4]));
        model.setMoreBigOrderAmt(StockStringUtil.getDecimalValue(str[5]));
        return model;
    }
}
